import os
import shutil
import cv2
from USCDatasetParse import cv_imread


def parseICCV(path, image_root):
    with open(path, 'r') as f:
        lines = f.readlines()
        nums_all = 0
        for line in lines:
            temp_boxes = []
            # print(line.strip().split('": '))
            line_list = line.strip().split('": ')
            if len(line_list) <2:
                continue
            temp_0 = line_list[0][1:]
            temp_1 = line_list[1][:-1]
            boxes = temp_1.split(', (')
            boxes[0] = boxes[0][1:]
            # name0, name1
            name_list = temp_0.split('/')
            name1 = name_list[-1]
            index_split = 0
            if 'eth' in path:
                image_path = os.path.join(image_root, name1[:-4] + '_0.png')
                index_split = 4
            else:
                image_path = os.path.join(image_root, name1)
                index_split = 1
            # print(image_path)
            image = cv_imread(image_path)
            # print(boxes)
            for temp_box in boxes:
                temp_box = temp_box[:-index_split]
                x1, y1, x2, y2 = temp_box.split(', ')
                x_min = int(x1)
                y_min = int(y1)
                x_max = int(x2)
                y_max = int(y2)
                cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (255, 0, 0))
                temp_boxes.append([x_min, y_min, x_max, y_max])
            # print(temp_0, temp_1)
            cv2.imshow('', image)
            cv2.waitKey(1)
            nums_all += 1
    print(nums_all)


if __name__ == '__main__':
    '''
    crossing-annot.idl ---- seq01-img-left
    linthescher-annot.idl ---- seq02-img-left
    eth01.idl ----- seq03-img-left
    eth02.idl ----- seq04-img-left
    sunny_day.idl ---- sunny_day-left
    '''

    path0 = "D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV '07 paper/crossing-annot.idl/crossing-annot.idl"
    image_root0 = 'D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV \'07 paper/seq01-img-left/'

    path1 = "D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV '07 paper/eth02.idl/eth02.idl"
    image_root1 = 'D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV \'07 paper/seq04-img-left/'

    path2 = "D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV '07 paper/eth01.idl/refined.idl"
    image_root2 = 'D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV \'07 paper/seq03-img-left/'

    path3 = "D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV '07 paper/sunny_day.idl/sunny_day.idl"
    image_root3 = 'D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV \'07 paper/sunny_day-img-left/'

    path4 = "D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV '07 paper/linthescher-annot.idl/linthescher-annot.idl"
    image_root4 = 'D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV \'07 paper/seq02-img-left/'

    path5 = "D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV '07 paper/lp.idl/lp.idl"
    image_root5 = 'D:/BaiduNetdiskDownload/行人检测/6_Dataset used in our ICCV \'07 paper/lp-left/'

    parseICCV(path0, image_root0)
    parseICCV(path1, image_root1)
    parseICCV(path2, image_root2)
    parseICCV(path3, image_root3)
    parseICCV(path4, image_root4)
    parseICCV(path5, image_root5)
